package com.yang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import org.jboss.logging.Logger;

@Controller
public class LotteryController {
    private static Logger logger = Logger.getLogger(LotteryController.class);

    /**
     * 抽奖
     * @param request
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = {"lucky_draw"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> luckyDraw(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        if(session.getAttribute("had") != null){
            map.put("status","error");
            logger.info("频繁抽奖");
            return map;
        }
        session.setAttribute("had",true);
        map.put("status","succeed");
        int num = (int) (Math.random()*100+1);

        if(num >=0 &&num<=3){
            map.put("info",0); //免单
            logger.info("免单 0 = "+num);
            return map;
        }else if(num >3 && num <= 15){
            map.put("info",1); //鸡米花
            logger.info("鸡米花 1 = "+num);
            return map;
        }else if(num >15 && num<=25){
            map.put("info",2); //蓝莓滑奶茶
            logger.info("蓝莓滑奶茶 2 = "+num);
            return map;
        }else if(num>25 && num<=47){
            map.put("info",3); //炸馒头
            logger.info("炸馒头 3 = "+num);
            return map;
        }else if(num>47 && num <= 70){
            map.put("info",5); //地瓜丸
            logger.info("地瓜丸 5 = "+num);
            return map;
        }else {
            map.put("info",4); //谢谢惠顾
            logger.info("谢谢惠顾 4 = "+num);
            return map;
        }
    }
}
